package member.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import member.domain.Member;
import member.service.MemberService;

public class SearchMemberServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String url = null;
		// 1.요청 파라미터 조회 - 검증
		String id = request.getParameter("id");
		// 요청 파라미터 검증 : 만약 id가 null이거나 "" 일 경우 - search_member.jsp로 이동
		if (id == null || id.trim().length() == 0) {
			request.setAttribute("message", "검색할 ID를 입력하세요.");
			url = "/member/search_member.jsp";
		} else {

			// 2.비지니스 로직 처리(ID로 회원찾기)
			// MemberService 조회처리 메소드 호출
			MemberService service = MemberService.getInstance();
			Member mem = service.searchMemberById(id);

			// 3.처리결과 응답 - member_info.jsp호출
			// request scope의 attribute로 조회한 Member객체를 binding(저장)
			request.setAttribute("member", mem);
			url = "/member/member_info.jsp";
		}
		RequestDispatcher rdp = request.getRequestDispatcher(url);// 요청
																	// Dispatch방법으로
		rdp.forward(request, response);

	}

}
